package com.tensquare.user.service;

import com.tensquare.entity.IdWorker;
import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author zzb
 * @version 1.0
 * @create 2020/12/22 21:43
 */
@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    //添加管理员
    public void add(Admin admin) {
        //设置管理员id
        admin.setId(idWorker.nextId()+"");
        //对密码加密
        String pwd = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(pwd);
        adminDao.save(admin);
    }

    //管理员登陆
    public Admin login(Admin admin) {
        //根据用户名查询用户
        Admin loginAdmin = adminDao.findByLoginname(admin.getLoginname());
        //判断是否有值
        if (loginAdmin != null && passwordEncoder.matches(admin.getPassword(),loginAdmin.getPassword())){
            return loginAdmin;
        }
        return null;
    }

    //删除管理员
    public void delete(String adminId) {
        adminDao.deleteById(adminId);
    }
}
